﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TexasHoldEmCroupier.UserControls;
using System.Globalization;
using TexasHoldEmCroupier.Properties;
using TexasHoldEmCroupier.Enumerators;

namespace TexasHoldEmCroupier.Views
{
    public partial class MainView : Form
    {
        #region Members
        private Stage _Stage = Stage.None;
        private decimal _LittleBlindValue = 50;
        private decimal _BigBlindValue = 100;
        private decimal _Pot = 0;
        private decimal _CurrentBet = 100;
        #endregion

        #region Properties
        public Stage Stage
        {
            get { return this._Stage; }
        }

        public decimal SmallBlind
        {
            get { return this._LittleBlindValue; }
            set { this._LittleBlindValue = value; }
        }

        public decimal BigBlind
        {
            get { return this._BigBlindValue; }
            set { this._BigBlindValue = value; }
        }

        public decimal Pot
        {
            get { return this._Pot; }
            set
            {
                this._Pot = value;

                this._labelPot.Text = String.Format(
                    CultureInfo.CurrentCulture,
                    "Pot: {0:N2}",
                    value);
            }
        }

        public decimal CurrentBet
        {
            get { return this._CurrentBet; }
            set
            {
                this._CurrentBet = value;

                this._labelCurrentBet.Text = String.Format(
                    CultureInfo.CurrentCulture,
                    "Current Bet: {0:N2}",
                    value);
            }
        }
        
        public PlayerStrip Dealer
        {
            get
            {
                foreach (PlayerStrip thisPlayerStrip in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayerStrip.IsDealer)
                    {
                        return thisPlayerStrip;
                    }
                }
                return null;
            }
            set
            {
                if (null != this.Dealer)
                {
                    this.Dealer.IsDealer = false;
                }
                value.IsDealer = true;
            }
        }

        public PlayerStrip LittleBlindPlayer
        {
            get
            {
                foreach (PlayerStrip thisPlayerStrip in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayerStrip.Blind.Equals(Blind.Little))
                    {
                        return thisPlayerStrip;
                    }
                }
                return null;
            }
            set
            {
                if (null != this.LittleBlindPlayer)
                {
                    this.LittleBlindPlayer.Blind = Blind.None;
                }
                value.Blind = Blind.Little;
            }
        }

        public PlayerStrip BigBlindPlayer
        {
            get
            {
                foreach (PlayerStrip thisPlayerStrip in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayerStrip.Blind.Equals(Blind.Big))
                    {
                        return thisPlayerStrip;
                    }
                }
                return null;
            }
            set
            {
                if (null != this.BigBlindPlayer)
                {
                    this.BigBlindPlayer.Blind = Blind.None;
                }
                value.Blind = Blind.Big;
            }
        }

        public PlayerStrip CurrentPlayer
        {
            get
            {
                foreach (PlayerStrip thisPlayer in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayer.IsActive)
                    {
                        return thisPlayer;
                    }
                }
                return null;
            }
            set
            {
                foreach (PlayerStrip thisPlayer in
                    this._panelPlayers.Controls)
                {
                    thisPlayer.IsActive = false;
                }

                if (null != value)
                {
                    value.IsActive = true;

                    this.CurrentPlayer.Activate(this);
                }
            }
        }

        public int NumberOfPlayers
        {
            get { return this._panelPlayers.Controls.Count; }
        }

        public int NumberOfAvailablePlayers
        {
            get
            {
                int theNumberOfAvailablePlayers = 0;

                foreach (PlayerStrip thisPlayer in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayer.IsAvailable)
                    {
                        ++theNumberOfAvailablePlayers;
                    }
                }

                return theNumberOfAvailablePlayers;
            }
        }

        public int NumberOfFoldedPlayers
        {
            get 
            {
                int theNumberOfFoldedPlayers = 0;

                foreach (PlayerStrip thisPlayer in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayer.Action.Equals(Action.Fold))
                    {
                        ++theNumberOfFoldedPlayers;
                    }
                }

                return theNumberOfFoldedPlayers;
            }
        }

        public int NumberOfAllInPlayers
        {
            get
            {
                int theNumberOfAllInPlayers = 0;

                foreach (PlayerStrip thisPlayer in
                    this._panelPlayers.Controls)
                {
                    if (thisPlayer.Action.Equals(Action.AllIn))
                    {
                        ++theNumberOfAllInPlayers;
                    }
                }

                return theNumberOfAllInPlayers;
            }
        }
        
        public FlowLayoutPanel panelPlayers
        {
            get { return this._panelPlayers; }
        }

        public DataGridView gridCommentary
        {
            get { return this._gridCommentary; }
        }
        #endregion

        #region Methods
        public MainView()
        {
            InitializeComponent();

            this.Pot = 0;
            this.CurrentBet = 0;
        }

        #region Private Helpers
        public void ClearPlayers()
        {
            this._panelPlayers.Controls.Clear();
        }

        public PlayerStrip GetPlayerStrip(
            int theIndex)
        {
            if (!this._panelPlayers.Controls.Count.Equals(0))
            {
                PlayerStrip theSelectedPlayerStrip =
                    this._panelPlayers.Controls[theIndex]
                        as PlayerStrip;

                if (null != theSelectedPlayerStrip)
                {
                    return theSelectedPlayerStrip;
                }
            }
            return null;
        }

        public PlayerStrip GetNextPlayer(
            PlayerStrip theSelectedPlayer)
        {
            int theIndex =
                this._panelPlayers.Controls.GetChildIndex(
                    theSelectedPlayer);

            theIndex = theIndex.Equals(this.NumberOfPlayers - 1)
                ? 0
                : theIndex + 1;

            return this.GetPlayerStrip(theIndex);
        }

        public PlayerStrip GetNextAvailablePlayer(
            PlayerStrip theSelectedPlayer)
        {
            int theIndex =
                this._panelPlayers.Controls.GetChildIndex(
                    theSelectedPlayer);

            theIndex = theIndex.Equals(this.NumberOfPlayers - 1)
                ? 0
                : theIndex + 1;

            for (int i = theIndex; i < this.NumberOfPlayers; i++)
            {
                if (this.GetPlayerStrip(i).IsAvailable)
                {
                    return this.GetPlayerStrip(i);
                }
            }
            
            for (int i = 0; i < theIndex; i++)
            {
                if (this.GetPlayerStrip(i).IsAvailable)
                {
                    return this.GetPlayerStrip(i);
                }
            }
            
            return null;
        }
        
        public void AdvanceStage()
        {
            switch (this._Stage)
            {
                case Stage.Hole:
                    this._Stage = Stage.Flop;
                    break;
                case Stage.Flop:
                    this._Stage = Stage.Turn;
                    break;
                case Stage.Turn:
                    this._Stage = Stage.River;
                    break;
                default:
                    this._Stage = Stage.Hole;
                    break;
            }

            if (!this._Stage.Equals(Stage.Hole))
            {
                this.CurrentBet = 0;
            }

            this._labelStage.Text = this._Stage.ToString();
        }

        public void StartGame()
        {
            this._Stage = Stage.None;
            this.Pot = 0;
            this.CurrentBet = this.BigBlind;
        }
        #endregion
        #endregion
    }
}
